<template>
    <div class="fromEdit">
        <div class="addbtn">
            <el-button type="primary" @click="addFrom"><Icon type="md-add" />新增风控配置</el-button>
            <el-button type="primary" @click="commitFrom"> 提 交 </el-button>
            <el-button type="" @click="$parent.showList()"> 返 回 </el-button>
        </div>

        <div>
            <el-collapse v-model="activeNames">
                <el-collapse-item v-for="(item,index) in fromList" :key="index+1" :title="'第('+(index+1)+')条'" :name="index+1">
                    <div>
                        <label for="">字段类型：</label>
                        <el-select v-model="item.riskType" clearable class="el-input" placeholder="请选择">
                            <el-option
                            v-for="(item,index) in typeOptions"
                            :key="index"
                            :label="item.riskType"
                            :value="item.riskType">
                            </el-option>
                        </el-select>
                    </div>
                    <div style="width: 15%;">
                        <label for="">是否启用：</label>
                        <template>
                            <el-radio v-model="item.enable" :label='label1'>是</el-radio>
                            <el-radio v-model="item.enable" :label='label2'>否</el-radio>
                        </template>
                    </div>
                    <div style="padding-left: 20px;width: 10%;">
                        <el-popconfirm :title="'确定删除第'+(index+1)+'条数据吗？'" icon-color="#f00" @confirm="deleteRowData(index)">
                            <el-button slot="reference" type="danger" size="mini" ><i style="font-size: 16px;" class="el-icon-delete"></i></el-button>
                        </el-popconfirm>
                    </div>
                    <div class="requestBodyClass" style="width: 100%;display: block;">
                        <el-divider content-position="left">
                            <el-button  size="mini" @click="addSelectrequestBody(index)"><Icon type="md-add" />下拉选项</el-button>
                        </el-divider>
                        <div class="requestBodyContent">
                            <div v-for="(chItem,chIndex) in item.param" :key="chIndex">
                                <p>
                                    <label for="">类型：</label>
                                    <el-select v-model="chItem.type" clearable class="el-input" placeholder="请选择">
                                        <el-option
                                        v-for="citem in ['input','param']"
                                        :key="citem"
                                        :label="citem"
                                        :value="citem">
                                        </el-option>
                                    </el-select>
                                </p>
                                <p>
                                    <label for="">key：</label>
                                    <el-input clearable v-model="chItem.key" placeholder="请输入内容"></el-input>
                                </p>
                                <p>
                                    <label for="">value：</label>
                                    <el-input clearable v-model="chItem.value" placeholder="请输入内容"></el-input>
                                </p>
                                <p>
                                    <el-popconfirm title="确定删除吗？" icon-color="#f00" @confirm="reduceListSelectFun(index,chIndex)">
                                        <i slot="reference" title="删除请求参数"  class="el-icon-delete-solid parDel" />
                                    </el-popconfirm>
                                    
                                </p>
                            </div>
                        </div>
                    </div>
                </el-collapse-item>
            </el-collapse>
        </div>
    </div>
</template>

<script>
  import request from '@/utils/request'
export default{
    props:{
        getSelectObj:{
            type:Object,
            default:()=>{
                return{}
            },
            require:true
        }
    },
    data(){
        return {    
            label1:true,
            label2:false,
            fromList:[],
            obj:{
                "riskType": "",
                "enable":true,
                "param": []
            },
            typeOptions:[],
            activeNames:''
        }
    },
    mounted(){
        request({
            url: '/ht-bpmn/risk/findRiskList',
            method: 'post',
            data:{
                "limit":1000,
                "page":1
            }
        }).then((res) => {
            if(res.code==200){
                this.typeOptions = res.data.records
            }
        })
        this.$nextTick(res=>{
            let parObj = JSON.parse(this.getSelectObj)
            if(parObj){
                this.fromList = parObj.riskConfig?JSON.parse(parObj.riskConfig):[]
            }
            if(this.fromList.length!=0){
                this.activeNames = []
                this.fromList.forEach((item,index)=>{
                    this.activeNames.push(index+1)
                })
            }
        })
    },   
    methods:{
        commitFrom(){
            let count = -1;
            outFor:for(let i=0;i<this.fromList.length;i++){
                let item = this.fromList[i]
                if(!item.riskType){
                    count = i
                    break
                }else if(item.param && item.param.length!=0){
                    for(let n=0;n<item.param.length;n++){
                        let citem = item.param[n]
                        if(!citem.key||!citem.value||!citem.type){
                            count = i
                            break outFor;
                        }    
                    }
                } 
            }
            if(count!=-1){
                this.$message.error(`请把第${count+1}条数据填写完整`)
                return 
            }else{
                this.commitjk(this.fromList)
            }
        },
        commitjk(v){
            request({
                    url: '/ht-bpmn/externalParameterEntity/saveOrUpdate',
                    method: 'post',
                    data:{
                        id:JSON.parse(this.getSelectObj).id,
                        riskConfig:JSON.stringify(v)
                    }
                }).then(res => {
                    if (res.code == 200) {
                        this.$parent.showList('true')
                        this.fromList = []
                        this.$message.success(res.msg)
                    } else {
                        this.$message.error(res.msg)
                    }
                })
        },
        deleteRowData(index){
            const a = JSON.parse(JSON.stringify(this.fromList))
            a.splice(index, 1)
            this.fromList = JSON.parse(JSON.stringify(a))
        },
        reduceListSelectFun(index,chIndex){
            const a = JSON.parse(JSON.stringify(this.fromList[index].param))
            a.splice(chIndex, 1)
            this.fromList[index].param = JSON.parse(JSON.stringify(a))
        },
   
        addSelectrequestBody(index){
            let a = JSON.parse(JSON.stringify(this.fromList))
            a[index].param.push({
                "type": "",
                "key": "",
                "value": ""
            })
            this.fromList = JSON.parse(JSON.stringify(a))
        },
        addFrom(){
            this.fromList.push({...this.obj})
            this.activeNames = this.fromList.length
        }
    }
}
</script>

<style lang="scss" scoped>
.fromEdit{
    position: relative;
    .addbtn{
        // position: fixed;
        background: #fff;
        width: 100%;
        z-index: 999;
        padding:0 0 10px 0;
    }
}
.parDel{
    color: red;
    line-height: 32px;
    margin-left: 10px;
    font-size: 18px;
    cursor: pointer;
}
>>>.el-collapse-item__wrap{
    padding-top: 20px;
}
>>>.el-collapse-item__header{
    background: #f2f5f8;
    padding-left: 10px;
}
>>>label.el-radio {
    width: 45px !important;
    display: inline-block;
    margin-right: 10px;
}
.requestBodyClass{
    .requestBodyContent{
        >div{
            display: flex;
            flex-direction: row;
            margin-bottom: 10px;
            p{
                display: flex;
                flex-direction: row;
                width: 20%;
            }
        }
    }
}
>>>.el-collapse-item__content {
    padding-bottom: 25px;
    font-size: 13px;
    color: #303133;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    >div{
        display: flex;
        flex-direction: row;
        width: 20%;
        margin-bottom: 10px;
        label{
            display: inline-block;
            width: 110px;
            text-align: right;
            line-height: 32px;
            font-size: 12px;
            font-weight: normal;
        }
    }
}
</style>